﻿<?php
#header("Content-Type: text/html; charset=UTF-8");

/**
* Initialisierungsdatei für die Biodiversity 2.0 API Erweiterung.
*
* @file
* @ingroup AkAPI
* @author pixilab.
*/

if (!defined('MEDIAWIKI')) {
	exit(1);
}

require_once ( "$IP/includes/api/ApiBase.php" );
require_once ( "db-functions.php" );


/** If set, AK will be available as an Extension */
if (!isset($wgAkAPI_ActionName) || is_null($wgAkAPI_ActionName)) {
	$wgAkAPI_ActionName = 'ak';
}

# Register the ApiMyExt class as handler for action=myext
$wgAPIModules[$wgAkAPI_ActionName] = 'AkAPI';


/**
* Web-based API for getting Properties and Property-Values (#ak)
*
* @ingroup SMW API
* @author pixilab
*/
class AkAPI extends ApiBase {
	
	################ Parameters ############################
	
	private static $PARAM_GET = 'get'; // Parameter f r Property oder Property-Value
	private static $PARAM_CAT = 'cat'; // Parameter f r die Kategorie
	private static $PARAM_SUB = 'sub'; // Parameter für ein Subjekt
	private $ACTION_NAME; // Action name in the API

	################ Konstruktor ###########################


	public function __construct($query, $moduleName) {
		parent :: __construct($query, $moduleName);
		global $wgAkAPI_ActionName;
		$this->ACTION_NAME = $wgAkAPI_ActionName;
		
	}
	
	################ API Description ########################
	
	public function mustBePosted() {
		return false;
	}
	
	public function isWriteMode() {
		return false;
	}
	
	protected function getAllowedParams() {
		return array(
			AkAPI::$PARAM_GET => null,
			AkAPI::$PARAM_CAT => null,
			AkAPI::$PARAM_SUB => null,
		);
	}
	
	protected function getParamDescription() {
		return array(
			AkAPI::$PARAM_GET => 'Properties or Property-Values to get',
			AkAPI::$PARAM_CAT => 'Page-Category',
			AkAPI::$PARAM_SUB => 'Page, to get Values for',
		);
	}
	
	protected function getDescription() {
		return 'Get all page-properties of a specific category';
	}
	
	protected function getExamples() {
		return array(
		   'api.php?action=' . $this->ACTION_NAME . '&' . AkAPI::$PARAM_CAT . '=Pflanzen' . '&' . AkAPI::$PARAM_GET . '=attributes',
		   'api.php?action=' . $this->ACTION_NAME . '&' . AkAPI::$PARAM_SUB . '=Apfel' . '&' . AkAPI::$PARAM_GET . '=attributes',
		   'api.php?action=' . $this->ACTION_NAME . '&' . AkAPI::$PARAM_GET . '=Blattform',
		   'api.php?action=' . $this->ACTION_NAME . '&' . AkAPI::$PARAM_GET . "=[[Blütezeit::März]][[Blütezeit::Mai]]||[[Blütezeit::März_bis_Mai]]",
		);
	}
	
	public function getVersion() {
		return __CLASS__ . '$' . AKAPI_VERSION; 
	}

	################# EXECUTE ############################## 
	public function execute() {
		
		// Parameter extrahieren
		$params = $this->extractRequestParams();
				
		// Parameter überprüfen, ob sie nicht leer sind
		$get = is_null($params[AkAPI::$PARAM_GET]) ? $this->dieUsage('You have to specify what you want to get (fe. get=attributes or get=value)', 0) : $params[AkAPI::$PARAM_GET];
		
		// Methoden für die Datenbank-Interaktionen
		$dbf = new DBF();
		
		// Status Tag
		$show;
				
		// Je nach Parameter entscheiden welche Methode ausgeführt werden soll 
		switch ($get) {
			case 'attributes':
				
				$sub = $params[AkAPI::$PARAM_SUB];
				if(is_null($sub)){
					// Parameter überprüfen, ob sie nicht leer sind
					$cat = is_null($params[AkAPI::$PARAM_CAT]) ? $this->dieUsage('You have to specify a page category (fe. Trees, Humans)', 1) : $params[AkAPI::$PARAM_CAT];
					$res = $dbf->getAttributes($cat);
				} else {
					$res = $dbf->getAttributesForSubject($sub);
				}	
				
				$show = "predicate";
				break;
			default:
				if(strpos($get, '::')) {
					$res = $dbf->getSubjects($get);
					$show = "subject";
					
				} else {					
					$res = $dbf->getValues($get);
					$show = "object";				
				}	
		}		

		$this->getResult()->addValue(array($this->ACTION_NAME, 'get'), AkAPI::$PARAM_GET, $get);
		$this->getResult()->setIndexedTagName($res, 'value');
		$this->getResult()->addValue(array($this->ACTION_NAME, $show), 'values', $res);

	}
	
}